﻿using _1._11._2015_BookReview.Data.Models;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity.Migrations;

namespace _1._11._2015_BookReview.Data
{
    public static class Seeder
    {
        public static void Seed(ApplicationDbContext db)
        {
            // Create roles, and an admin
            UserStore<ApplicationUser> userStore = new UserStore<ApplicationUser>(db);
            UserManager<ApplicationUser> userManager = new UserManager<ApplicationUser>(userStore);
            
            RoleStore<Role> roleStore = new RoleStore<Role>(db);
            RoleManager<Role> roleManager = new RoleManager<Role>(roleStore);

            if(!roleManager.RoleExists("General"))
            {
                roleManager.Create(new Role 
                {
                     Name = "General"
                });
            }
            if(!roleManager.RoleExists("Admin"))
            {
                roleManager.Create(new Role 
                { 
                    Name = "Admin"
                });
            }

            // first add a manager
            ApplicationUser louis = userManager.FindByName("louism817@gmail.com");

            if (louis == null)
            {
                louis = new ApplicationUser
                {
                    FirstName = "Louis",
                    LastName = "Murphy",
                    Email = "louism817@gmail.com",
                    UserName = "louism817@gmail.com"
                };
                userManager.Create(louis, "123456");
                userManager.AddToRole(louis.Id, "Admin");
            }

            // first add a manager
            ApplicationUser joe = userManager.FindByName("joe@user.com");

            if (joe == null)
            {
                joe = new ApplicationUser
                {
                    FirstName = "Joe",
                    LastName = "User",
                    Email = "joe@user.com",
                    UserName = "joe@user.com"
                };
                userManager.Create(joe, "123456");
                userManager.AddToRole(joe.Id, "General");
            }

            // if we don't have any books, add some

            db.Books.AddOrUpdate(b => new { b.Title, b.AuthorFName, b.AuthorLName },
                new Book
                {
                    Title = "Book 1",
                    AuthorFName = "A1First",
                    AuthorLName = "A1Last"
                },
                new Book
                {
                    Title = "Book 2",
                    AuthorFName = "A2First",
                    AuthorLName = "A2Last"
                },
                new Book
                {
                    Title = "Book 3",
                    AuthorFName = "A3First",
                    AuthorLName = "A3Last"
                }

                );

            db.SaveChanges();

        }
    }
}
